package com.example.demo.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.demo.Mapper.FarmInfoMapper;
import com.example.demo.base.Result;
import com.example.demo.entity.FarmInfo;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.demo.Mapper.AgriProductMapper;
import com.example.demo.base.Result;
import com.example.demo.entity.AgriProduct;
import com.example.demo.entity.Commodity;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

@RestController
@RequestMapping("/farmInfo")
public class FarmInfoController {
    @Resource
    FarmInfoMapper farmInfoMapper;

    /**
     * 获取数据库全部数据
     *
     * @return
     */
    @GetMapping("/all")
    public Result<?> findAll() {
        return Result.success(farmInfoMapper.selectList(null));
    }

    /**
     * 分页获取数据库数据
     *
     * @param pageNum
     * @param pageSize
     * @return
     */

    @GetMapping(value = "/pageAll")
    public Result<?> findPageF(@RequestParam(defaultValue = "1") Integer pageNum,
                               @RequestParam(defaultValue = "10") Integer pageSize,
                               @RequestParam String tenantId) {
        LambdaQueryWrapper<FarmInfo> wrapper = Wrappers.<FarmInfo>lambdaQuery();
        wrapper.eq(FarmInfo::getTenantId, Integer.valueOf(tenantId));
        Page<FarmInfo> farmInfoPage = farmInfoMapper.selectPage(new Page<>(pageNum, pageSize), wrapper);
        return Result.success(farmInfoPage);
    }

    /**
     * 新增
     *
     * @param farmId
     * @param farmName
     * @param farmTel
     * @param farmLocation
     * @param farmProduct
     * @param farmProfile
     * @return
     */

    @GetMapping(value = "/add")
    public Result<?> saveA(
                           @RequestParam String farmId,
                           @RequestParam String farmName,
                           @RequestParam String farmTel,
                           @RequestParam String farmLocation,
                           @RequestParam String farmProduct,
                           @RequestParam String farmProfile,
                           @RequestParam String tenantId) {
        if (farmName.isEmpty() || farmTel.isEmpty() || farmLocation.isEmpty() || farmProduct.isEmpty() || farmProfile.isEmpty()) {
            return Result.error(0, "文本不能为空");
        }
        FarmInfo farmInfo = new FarmInfo();
        farmInfo.setFarmId(Integer.valueOf(farmId));
        farmInfo.setFarmName(farmName);
        farmInfo.setFarmTel(farmTel);
        farmInfo.setFarmLocation(farmLocation);
        farmInfo.setFarmProduct(farmProduct);
        farmInfo.setFarmProfile(farmProfile);
        farmInfo.setTenantId(Integer.valueOf(tenantId));
        farmInfoMapper.insert(farmInfo);
        return Result.success();
    }

    /**
     * 修改方法
     * //* @param id
     *
     * @param farmId
     * @param farmName
     * @param farmTel
     * @param farmLocation
     * @param farmProduct
     * @param farmProfile
     * @return
     */
    @GetMapping(value = "/update")
    public Result<?> updateF(
                             @RequestParam String farmId,
                             @RequestParam String farmName,
                             @RequestParam String farmTel,
                             @RequestParam String farmLocation,
                             @RequestParam String farmProduct,
                             @RequestParam String farmProfile,
                             @RequestParam String tenantId) {
        if (farmName.isEmpty() || farmTel.isEmpty() || farmLocation.isEmpty() || farmProduct.isEmpty() || farmProfile.isEmpty()) {
            return Result.error(0, "文本不能为空");
        }
        FarmInfo farmInfo = new FarmInfo();
        //farmInfo.setId(Integer.valueOf(id));
        farmInfo.setFarmId(Integer.valueOf(farmId));
        farmInfo.setFarmName(farmName);
        farmInfo.setFarmTel(farmTel);
        farmInfo.setFarmLocation(farmLocation);
        farmInfo.setFarmProduct(farmProduct);
        farmInfo.setFarmProfile(farmProfile);
        farmInfo.setTenantId(Integer.valueOf(tenantId));
        farmInfoMapper.updateByMultiId(farmInfo);
        return Result.success();
    }

    /**
     * 根据farmId单个查询
     *
     * @param name
     * @return
     */

    @GetMapping("/findOne")
    public Result<?> findOne(@RequestParam String name,
                             @RequestParam String tenantId) {
        LambdaQueryWrapper<FarmInfo> wrapper = Wrappers.<FarmInfo>lambdaQuery();
        wrapper.eq(FarmInfo::getFarmName, name)
                .eq(FarmInfo::getTenantId, Integer.valueOf(tenantId));
        FarmInfo farmInfo = farmInfoMapper.selectOne(wrapper);
        if (farmInfo != null) {
            return Result.success(farmInfo);
        }
        return Result.error(0, "没有查询到相关农场属性");
    }
    @GetMapping("/findById")
    public Result<?> findByFarmId(@RequestParam String farmId,
                                  @RequestParam String tenantId) {
        FarmInfo farmInfo = new FarmInfo();
        farmInfo.setFarmId(Integer.valueOf(farmId));
        farmInfo.setTenantId(Integer.valueOf(tenantId));
        FarmInfo tmp = farmInfoMapper.selectByMultiId(farmInfo);
        List<FarmInfo> farmInfoList = new ArrayList<>();
        if (tmp != null) {
            farmInfoList.add(tmp);
            return Result.success(farmInfoList);
        }
        return Result.error(0, "没有查询到相关数据");
    }

    /**
     * 根据farmName模糊查询
     *
     * @param farmName
     * @return
     */
    @GetMapping("/findByFarmName")
    public Result<?> findByFarmName(@RequestParam(defaultValue = "1") Integer pageNum,
                                    @RequestParam(defaultValue = "10") Integer pageSize,
                                    @RequestParam String farmName,
                                    @RequestParam String tenantId) {
        LambdaQueryWrapper<FarmInfo> wrapper = Wrappers.<FarmInfo>lambdaQuery();
        wrapper.like(FarmInfo::getFarmName, farmName)
                .eq(FarmInfo::getTenantId, tenantId);
        Page<FarmInfo> farmInfoPage = farmInfoMapper.selectPage(new Page<>(pageNum, pageSize), wrapper);
        if (farmInfoPage.getTotal() > 0)
            return Result.success(farmInfoPage);
        return Result.error(0, "没有查询到相关数据");
    }

    /**
     * 根据farmId删除
     *
     * @param farmId
     * @return
     */
    @DeleteMapping("/delById")
    public Result<?> delete(@RequestParam String farmId,
                            @RequestParam String tenantId) {
        FarmInfo farmInfo = new FarmInfo();
        farmInfo.setFarmId(Integer.valueOf(farmId));
        farmInfo.setTenantId(Integer.valueOf(tenantId));
        int res = farmInfoMapper.deleteByMultiId(farmInfo);
        if (res > 0)
            return Result.success();
        return Result.error(0, "删除失败");
    }
    /*@DeleteMapping("/delById/{farmId}")
    public Result<?> delete(@PathVariable String farmId) {
        int res = farmInfoMapper.deleteById(farmId);
        if (res > 0)
            return Result.success();
        return Result.error(0, "删除失败");
    }*/


}
